package org.goldfish.cookied_chatroom02.servlet;

import jakarta.servlet.ServletContextEvent;
import jakarta.servlet.ServletContextListener;
import jakarta.servlet.annotation.WebListener;
import org.goldfish.cookied_chatroom02.dao.DaoManager;
import org.goldfish.cookied_chatroom02.utils.Utils;

import java.sql.SQLException;

@WebListener
public class ChatRoomContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        Utils.initLoginSessionRecorder(sce.getServletContext());
        try {
            DaoManager.init(sce.getServletContext());
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        try {
            DaoManager.destroy();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        ServletContextListener.super.contextDestroyed(sce);
    }
}
